package com.yinet.system.service.impl;

import com.yinet.common.core.domain.Ztree;
import com.yinet.system.domain.TouchScreenGuidance;
import com.yinet.system.mapper.TouchScreenGuidanceMapper;
import com.yinet.system.service.TouchScreenGuidanceService;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.*;

/**
 * 办事指南 业务层处理
 *
 * @author ruoyi
 */
@Service
public class TouchScreenGuidanceServiceImpl implements TouchScreenGuidanceService {

    @Autowired
    private TouchScreenGuidanceMapper guidanceMapper;

    @Override
    public List<TouchScreenGuidance> selectGuidanceList() {
        List<TouchScreenGuidance> guidanceList = guidanceMapper.selectGuidanceList();
        return guidanceList;
    }

    @Override
    public TouchScreenGuidance selectGuidanceById(Long id) {
        TouchScreenGuidance touchScreenGuidance = guidanceMapper.selectGuidanceById(id);
        return touchScreenGuidance;
    }

    @Override
    public int updateGuidance(TouchScreenGuidance guidance) {
        int i = guidanceMapper.updateGuidance(guidance);
        return i;
    }

    @Override
    public int insertGuidance(TouchScreenGuidance guidance) {
        int i = guidanceMapper.insertGuidance(guidance);
        return i;
    }

    @Override
    public int selectCountGuidanceByParentId(Long parentId) {
        int i = guidanceMapper.selectCountGuidanceByParentId(parentId);
        return i;
    }

    @Override
    public int deleteGuidanceById(Long guidanceId) {
        int i = guidanceMapper.deleteGuidanceById(guidanceId);
        return i;
    }

    @Override
    public List<Ztree> guidanceTreeData() {
        List<TouchScreenGuidance> guidanceList = guidanceMapper.selectGuidanceList();
        List<Ztree> ztrees = initZtree(guidanceList);
        return ztrees;
    }

    @Override
    public List<TouchScreenGuidance> selectGuidanceListByIdAndType(Long id, String type) {
        List<TouchScreenGuidance> guidanceList = guidanceMapper.selectGuidanceList();
        return findChildGuidance(id, type, guidanceList, true);
    }

    @Override
    public List<TouchScreenGuidance> selectGuidanceListById(Long id) {
        List<TouchScreenGuidance> childList = guidanceMapper.selectGuidanceByParentId(id);
        List<TouchScreenGuidance> AllList = guidanceMapper.selectGuidanceNotParentId(id);
        for (int i = 0; i < childList.size(); i++) {
            List<TouchScreenGuidance> temp = new ArrayList<>();
            for (int j = 0; j < AllList.size(); j++) {
                if (AllList.get(j).getParentId().equals(childList.get(i).getTsGuidanceId())) {
                    temp.add(AllList.get(j));
                }
            }
            childList.get(i).setChildren(temp);
        }
        return childList;
    }

    /**
     * 对象转菜单树
     *
     * @param guidanceList 菜单列表
     * @return 树结构列表
     */
    public List<Ztree> initZtree(List<TouchScreenGuidance> guidanceList) {
        List<Ztree> ztrees = new ArrayList<Ztree>();
        for (TouchScreenGuidance guidance : guidanceList) {
            if (guidance.getGuidanceType().equals("M")) {
                Ztree ztree = new Ztree();
                ztree.setId(guidance.getTsGuidanceId());
                ztree.setpId(guidance.getParentId());
                ztree.setName(guidance.getGuidanceName());
                ztree.setTitle(guidance.getGuidanceName());
                ztrees.add(ztree);
            }
        }
        return ztrees;
    }

    /**
     * 根据GuidanceId和Type获取所有子节点
     */
    public List<TouchScreenGuidance> findChildGuidance(Long id, String type, List<TouchScreenGuidance> list, boolean isFirst) {
        List<TouchScreenGuidance> tempList = new ArrayList<>();
        for (TouchScreenGuidance item : list) {//循环遍历父ID为id的项
            Long parentId = item.getParentId();
            String nowType = item.getGuidanceType();
            if (isFirst) {
                if (parentId.equals(id)) {//如果是子项就添加进列表
                    if (nowType.equals("M")) {
                        List<TouchScreenGuidance> childGuidance = findChildGuidance(item.getTsGuidanceId(), type, list, false);
                        if (childGuidance.size() > 0) {
                            item.setChildren(childGuidance);
                            tempList.add(item);
                        }
                    } else if (nowType.equals(type)) {
                        tempList.add(item);
                    }
                }
            } else {
                if (parentId.equals(id) && nowType.equals(type)) {//如果是子项就添加进列表
                    item.setChildren(findChildGuidance(item.getTsGuidanceId(), type, list, false));
                    tempList.add(item);
                }
            }

        }
        return tempList;
    }

    @Override
    public List<TouchScreenGuidance> selectParentGuidanceList() {
        return guidanceMapper.selectParentGuidanceList();
    }

    @Override
    public String testGuidance() throws IOException {
        int changeRow = getExcel();
        return "影响了 " + changeRow + "行";
    }

    public int getExcel() throws IOException {
        HSSFWorkbook hssfWorkbook = new HSSFWorkbook(new FileInputStream("C:\\Users\\kkexc\\Desktop\\井陉触摸屏\\197项)(1).xls"));
        HSSFSheet sheet = hssfWorkbook.getSheetAt(0);
        int lastRowNum = sheet.getLastRowNum();
        int changeRow = 0;
        for (int i = 0; i <= lastRowNum; i++) {
            HSSFRow row = sheet.getRow(i);
            String name = row.getCell(0).getStringCellValue();
            String parent = row.getCell(1).getStringCellValue();
            TouchScreenGuidance guidance = new TouchScreenGuidance();
            guidance.setTsGuidanceId(Long.valueOf(i));
            if (!name.isEmpty()) {
                TouchScreenGuidance guidance1 = guidanceMapper.selectGuidanceByName(parent);
                guidance.setParentId(guidance1.getTsGuidanceId());
                guidance.setGuidanceName(name);
                guidance.setGuidanceType("Z");
                guidance.setSubmitTime(new Date());
                guidance.setOrderNum(String.valueOf(i));
                changeRow = changeRow + guidanceMapper.insertGuidance(guidance);
            }
        }
        return changeRow;
    }
}
